import { STable } from '@/components'

export default {
  components: {
    STable
  },
  data () {
    return {
      queryParam: {},
      queryParam1: {},
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam, this.$route.params, this.$route.query)
        console.log('loadData request parameters:', requestParameters)
        return this.listAction(Object.assign({},requestParameters,{apply_status:0}))
          .then(res => {
            return res.data
          })
      },
      loadData1: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam1, this.$route.params, this.$route.query)
        console.log('loadData request parameters:', requestParameters)
        return this.listAction(Object.assign({},requestParameters,{apply_status:1}))
          .then(res => {
            return res.data
          })
      },
      pagination: { 'show-total': total => `共 ${total} 条`, showSizeChanger: false }
    }
  },
  methods: {
    handleDetail (record) {
      this.$refs.createDrawer.show(record)
    },
    handleAdd () {
      this.$refs.createModal.show()
    },
    handleEdit (record) {
      this.$refs.createModal.show(record)
    },
    handleDelete (value) {
      this.$confirm({
        title: '你确认进行删除操作吗?',
        content: h => <div style="color:red;">删除后将无法进行撤销</div>,
        okType: 'danger',
        onOk: () =>
          this.delAction({ id: value.id })
            .then(res => {
              this.handleSearch()
              this.$message.success('删除成功')
            })
            .catch(() => {})
      })
    },
    handleReset(){
      this.queryParam = {}
      this.queryParam1 = {}
      this.$refs.table.refresh(true)
    },
    handleSearch (boolean) {
      this.$refs.table.refresh(boolean)
    },
    handleSearch1 (boolean) {
      this.$refs.table1.refresh(boolean)
    },
    handleExportXls (fileName) {
      if (!fileName || typeof fileName !== 'string') {
        fileName = '导出文件'
      }
      const param = this.queryParam
      console.log('导出参数', param)
      this.downAction(param).then((data) => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }), fileName + '.xls')
        } else {
          const url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }))
          const link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
      })
    }
  }
}
